
/**
 * Date:2019年6月3日下午2:14:27
 * Copyright (c) 2019, www.bwbroad.com All Rights Reserved.
 *
*/

package com.xue.im.common;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import javax.swing.filechooser.FileSystemView;

import com.xue.im.common.vo.Header;
import com.xue.im.common.vo.Message;
import com.xue.im.common.vo.User;

/**
 * Description: 工具和常量 <br/>
 * Date: 2019年6月3日 下午2:14:27 <br/>
 * 
 * @author xuejianxin
 */
public class IMUtil {

	public static final String USER_NAME = "U";// 用户名
	public static final String PASSWORD = "P";// 密码
	public static final String SENDER = "S";// 发送者
	public static final String RECEIVER = "R";// 接收者
	public static final String ALL = "__ALL__";// 所有人

	public static final int USER_STATUS_OFFLINE = 0;// 下线
	public static final int USER_STATUS_ONLINE = 1;// 在线
	public static final int USER_STATUS_REPEAT_LOGIN = 2;// 重复登录
	public static final int USER_STATUS_PASSWORD_NOT_CORRECT = 3;// 密码不正确
	public static final int USER_STATUS_USER_NOT_EXIST = 4;// 用户不存在
	private static final String[] USER_STATUS_MSG = { "下线", "在线", "重复登录", "密码不正确", "用户不存在" };

	public static final int MESSAGE_LEN_1M = 1024 * 1024;
	public static final int MESSAGE_LEN_M = 10;
	public static final int MESSAGE_LEN = MESSAGE_LEN_1M * MESSAGE_LEN_M;

	public static String getUserStatusMsg(int status) {
		return USER_STATUS_MSG[status];
	}

	// 检查消息类型
	public static boolean checkMessageType(Message message, MessageType t) {
		return null != message && message.getHeader().getType() == t.value();
	}

	// 构建空消息
	public static Message buildMessage(MessageType t) {
		Message message = new Message();
		Header h = new Header();
		h.setType(t.value());
		message.setHeader(h);
		return message;
	}

	public static Message buildMessageObject(MessageType t, Object body) {
		Message message = buildMessage(t);
		message.setBody(body);
		return message;
	}

	public static Message buildMessage(MessageType t, byte body) {
		return buildMessageObject(t, body);
	}

	public static Message buildMessage(MessageType t, String body) {
		return buildMessageObject(t, body);
	}

	public static Message buildMessage(MessageType t, User sender, User receiver, String body) {
		return buildMessage(t, body).addOptions(SENDER, sender.getUserid()).addOptions(IMUtil.RECEIVER,
				receiver.getUserid());
	}

	public static String getServerIp() {
		String ip = "127.0.0.1";
		return ip;
	}

	public static int getServerPort() {
		return 9098;
	}

	private static final DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
	private static final DateTimeFormatter df1 = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");

	public static final String getNow() {
		return LocalDateTime.now().format(df);
	}

	public static final String getNow1() {
		return LocalDateTime.now().format(df1);
	}

	public static String formatMessage(String username, String msg) {
		return String.format("[%s] %s\r\n  %s", username, getNow(), msg);
	}

	// 消息类型
	public enum MessageType {
		LOGIN_REQ((byte) 1)// 登录请求
		, LOGIN_RESP((byte) 2)// 登录响应
		, HEARTBEAT_REQ((byte) 3)// 心跳请求
		, HEARTBEAT_RESP((byte) 4)// 心跳响应
		, TEXT_REQ((byte) 5)// 文本消息请求
		, TEXT_RESP((byte) 6)// 文本消息响应
		, USERLIST_REQ((byte) 7)// 用户列表请求
		, USERLIST_RESP((byte) 8)// 用户列表响应
		, USER_ONLIE_RESP((byte) 9)// 用户上下线响应,服务器端广播

		, FILE_REQ((byte) 10)// 文件消息请求
		, FILE_RESP((byte) 11)// 文件消息响应
		;
		private byte value;

		private MessageType(byte value) {
			this.value = value;
		}

		public byte value() {
			return value;
		}

		public static String name(byte no) {
			for (MessageType mt : MessageType.values()) {
				if (mt.value() == no) {
					return no + ":" + mt.toString();
				}
			}
			return no + ":" + "未知消息类型";
		}

	}

	public static byte[] toByteArray(File file) throws Exception {
		File f = file;
		if (!f.exists()) {
			throw new FileNotFoundException("file not exists");
		}
		ByteArrayOutputStream bos = new ByteArrayOutputStream((int) f.length());
		BufferedInputStream in = null;
		try {
			in = new BufferedInputStream(new FileInputStream(f));
			int buf_size = 1024;
			byte[] buffer = new byte[buf_size];
			int len = 0;
			while (-1 != (len = in.read(buffer, 0, buf_size))) {
				bos.write(buffer, 0, len);
			}
			return bos.toByteArray();
		} catch (IOException e) {
			e.printStackTrace();
			throw e;
		} finally {
			try {
				in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			bos.close();
		}
	}

	public static void toFile(String path, byte[] bs) {
		try {
			OutputStream out = new FileOutputStream(path);
			InputStream is = new ByteArrayInputStream(bs);
			byte[] buff = new byte[1024];
			int len = 0;
			while ((len = is.read(buff)) != -1) {
				out.write(buff, 0, len);
			}
			is.close();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static String getFileDir() {
		// 获取桌面
		File desktopFile = FileSystemView.getFileSystemView().getHomeDirectory();
		return desktopFile.getAbsolutePath() + File.separator;
	}

}
